package com.china_goldcard.myconfirmdialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 我的确认对话框
 */

public class MyConfirmDialog extends Dialog {

    private TextView cancelBtn;

    private TextView sureBtn;

    private TextView titleTV;

    private TextView msgTV;

    private String sureTitle = "";

    private String cancelTitle = "取消";

    private String message;

    private String titleTx = "提示";

    private RelativeLayout rootView;

    private View line;

    private OnBtnClickListener btnClickListener;

    private float titleH = 44;

    private float btnH = 44;

    private float msgH = 44;

    private boolean canCancelOutside = false;

    public MyConfirmDialog(Context context,boolean canCancelOutside) {
        super(context,R.style.MyConfirmDialog);
        this.canCancelOutside = canCancelOutside;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(canCancelOutside);
        initView();
        setCancelable(canCancelOutside);
        initEvent();
        updateUI();
    }

    private void updateUI(){
        cancelBtn.setText(cancelTitle);
        sureBtn.setText(sureTitle);
        titleTV.setText(titleTx);
        msgTV.setText(message);
        float dialogTitleH = dip2px(getContext(),titleH);
        float dialogBtnH = dip2px(getContext(),btnH);
        float dialogMsgH = dip2px(getContext(),msgH);
        rootView.getLayoutParams().height = (int)(dialogTitleH + dialogBtnH + dialogMsgH);
        if (sureTitle ==null || sureTitle.trim().isEmpty()){
            sureBtn.getLayoutParams().width = 0;
            cancelBtn.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
            line.setVisibility(View.GONE);
            sureBtn.setVisibility(View.GONE);
        }else{
            line.setVisibility(View.VISIBLE);
        }
    }

    public void setSureTitle(String sureTitle) {
        this.sureTitle = sureTitle;
    }

    public void setCancelTitle(String cancelTitle) {
        this.cancelTitle = cancelTitle;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setTitleTx(String titleTx) {
        this.titleTx = titleTx;
    }

    public void setMsgH(float msgH) {
        this.msgH = msgH;
    }

    public void setBtnClickListener(OnBtnClickListener btnClickListener) {
        this.btnClickListener = btnClickListener;
    }

    /**
     * 初始化视图
     */
    private void initView(){
        setContentView(R.layout.my_confirm_dialog);
        cancelBtn = (TextView) findViewById(R.id.my_confirm_cancel_btn);
        sureBtn = (TextView) findViewById(R.id.my_confirm_sure_btn);
        line = (View) findViewById(R.id.seperator_line_2);
        titleTV = (TextView) findViewById(R.id.my_confirm_dialog_title);
        msgTV = (TextView)findViewById(R.id.my_confirm_dialog_msg);
        rootView = (RelativeLayout)findViewById(R.id.my_confirm_dialog_root);
    }

    /**
     * 初始化事件
     */
    private void initEvent(){
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (btnClickListener!=null){
                    btnClickListener.onCancelBtnClick(v,MyConfirmDialog.this);
                }
            }
        });
        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnClickListener!=null){
                    btnClickListener.onConfirmBtnClick(v,MyConfirmDialog.this);
                }
            }
        });
    }

    public interface OnBtnClickListener{
        void onCancelBtnClick(View v,MyConfirmDialog dialog);
        void onConfirmBtnClick(View v,MyConfirmDialog dialog);
    }

    /**
     * Builder for arguments
     */
    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String cancelTxt;
        private String sureTxt;
        private float msgH = 44;
        private OnBtnClickListener onBtnClickListener;
        private boolean canCancelOutside;

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setCanCancelOutside(boolean canCancelOutside){
            this.canCancelOutside = canCancelOutside;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setCancelText(String cancelTxt) {
            this.cancelTxt = cancelTxt;
            return this;
        }

        public Builder setSureText(String sureTxt) {
            this.sureTxt = sureTxt;
            return this;
        }

        public Builder setMsgH(float msgH){
            this.msgH = msgH;
            return this;
        }

        public Builder setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
            this.onBtnClickListener = onBtnClickListener;
            return this;
        }

        public MyConfirmDialog build() {
            MyConfirmDialog dialog = new MyConfirmDialog(context,canCancelOutside);
            dialog.setCancelTitle(cancelTxt);
            dialog.setSureTitle(sureTxt);
            dialog.setMessage(message);
            String dialogTitle = title != null && !title.trim().isEmpty() ? title.trim() : "提示";
            dialog.setTitleTx(dialogTitle);
            dialog.setMsgH(msgH);
            dialog.setBtnClickListener(onBtnClickListener);
            return dialog;
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     * @param context
     * @param dpValue 要转换的dp值
     */
    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     * @param context
     * @param pxValue 要转换的px值
     */
    private int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
